package com.tianwen.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianwen.management.entity.Role;
import com.tianwen.management.entity.User;
import com.tianwen.management.entity.UserRolePermission;
import com.tianwen.management.service.RoleService;
import com.tianwen.management.mapper.RoleMapper;
import com.tianwen.management.service.UserRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* @author Administrator
* @description 针对表【role】的数据库操作Service实现
* @createDate 2023-05-12 15:34:17
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{
@Resource
private RoleMapper roleMapper;
@Resource
private UserRolePermissionService userRolePermissionService;
    @Override
    public List<Role> page(Integer page, Integer pageSize, String name) {
        QueryWrapper<Role> queryWrap=new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(name)){
            queryWrap.like("name",name);
        }
        queryWrap.orderByDesc("id" );
        Page<Role> pageInfo=new Page<>(page,pageSize);
        List<Role> roleList = roleMapper.selectPage(pageInfo, queryWrap).getRecords();
        return roleList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeAndPermission(Long id) {
              roleMapper.deleteById(id);
              QueryWrapper<UserRolePermission> queryWrap=new QueryWrapper<>();
              queryWrap.eq("role_id",id);
              userRolePermissionService.remove(queryWrap);
              return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAndPermission(Role role) {
                 roleMapper.insert(role);
                 role.getPermissionList().forEach(permission -> {
                     UserRolePermission userRolePermission=new UserRolePermission();
                     userRolePermission.setRoleId(role.getId());
                     userRolePermission.setPermissionId(permission.getId());
                     userRolePermission.setUserId(null);
                     userRolePermission.setIsUser(0);
                     userRolePermissionService.save(userRolePermission);
                 });
                 return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAndPermission(Role role) {
        roleMapper.updateById(role);
        QueryWrapper<UserRolePermission> queryWrap=new QueryWrapper<>();
        queryWrap.eq("role_id",role.getId());
        List<UserRolePermission> userRolePermissionList = userRolePermissionService.list(queryWrap);
        Set<Long> userIds=new HashSet<>();
        userRolePermissionList.stream().forEach(userRolePermission -> {
            if(userRolePermission.getIsUser()==1){
                userIds.add(userRolePermission.getUserId());
            }
        });
        userRolePermissionService.remove(queryWrap);

        role.getPermissionList().forEach(permission -> {
            UserRolePermission userRolePermission=new UserRolePermission();
            userRolePermission.setRoleId(role.getId());
            userRolePermission.setPermissionId(permission.getId());
            userRolePermission.setUserId(null);
            userRolePermission.setIsUser(0);
            userRolePermissionService.save(userRolePermission);
        });

        for (Long userId : userIds) {
            role.getPermissionList().forEach(permission -> {
                UserRolePermission userRolePermission = new UserRolePermission();
                userRolePermission.setRoleId(role.getId());
                userRolePermission.setPermissionId(permission.getId());
                userRolePermission.setUserId(userId);
                userRolePermission.setIsUser(1);
                userRolePermissionService.save(userRolePermission);
            });

        }
        return true;
    }

}




